********************************************************************************
* BACKGROUND
********************************************************************************
/*	This code implements a set of robustness checks for the analysis of the
	Matter of A-B- decision. It begins by rejecting a difference-in-differences
	design due to violations of the parallel trends assumption between 
	likely treated and untreated groups, opting instead for a regression 
	discontinuity in time framework. The code then evaluates whether 
	the decision had different effects on pro se versus represented applicants,
	showing that effects were concentrated among the latter. A falsification test 
	is conducted using a sample unlikely to be affected by the decision. Additional
	placebo tests shift the treatment date earlier to assess whether effects are
	tied to the actual decision date. The code also examines sensitivity to 
	bandwidth choices and adds controls for potential confounding factors such 
	as the Castro-Tum decision, circuit-level legal variation, and immigration 
	judge (IJ) characteristics and work experience. Finally, a series of functional
	form robustness checks—including linear, polynomial (squared/cubed), and spline
	models with varying numbers of breakpoints—are performed to test the 
	consistency of results under alternative model specifications*/
 
********************************************************************************
* READ IN DATA AND SET WORKING DIRECTORY TO SAVE OUTPUT FROM ANALYSIS
********************************************************************************
use matter_ab_robustness_data, clear 
cd "output\robustness_checks"
	
********************************************************************************
* PARALLEL TRENDS ANALYSIS 
********************************************************************************
/*	 We originally tried constructing a control group that Matter of A-B- was
	 unlikely to affect—namely, noncitizens without legal representation who 
	 were not making asylum claims and were not from Mexico or the Northern 
	 Triangle—we found that the parallel-trends assumption did not hold.  
	 Our comparison of temporal trends between this would-be control group 
	 and the would-be treatment group revealed a visually notable and 
	 statistically significant divergence between the groups in the months
	 immediately before Matter of A-B-.
	 
	 Given this divergence, we do not believe it is reasonable to assume 
	 that the treatment and control groups would have followed similar trends 
	 over time if Matter of A-B- had not been implemented. Without satisfying
	 this key assumption of parallel trends, we believe using a DiD approach 
	 would provide misleading estimates. 
	 
	 RDiT, in contrast, provides a conceptually cleaner strategy that 
	 examines changes within the same group around the Matter of A-B- cutoff. 
	 Thus, RDiT offers a more reliable method for isolating the impact of Matter
	 of A-B- on asylum applicants who are most likely to be affected, without 
	 the complications presented by a DiD approach that we highlighted above.*/

log using matter_ab_parallel_trend_comparision.log, replace 
	
	gen shouldnt_be_affected = ///
		app_asylum==0 & nationality==0 & atty==0
		
	gen should_be_affected = ///
		app_asylum==1 & nationality!=0 & atty==1

	gen pt_ind = 1 if should_be_affected==1
	replace pt_ind = 0 if shouldnt_be_affected==1

	reg removal_decision ///
		i.p_comp_month##i.pt_ind ///
		if days_from_ab>-61 & days_from_ab<1 & ///
		is_missing==0 & IJ_pre_post_AB==1, robust
			
log close 
	
	margins i.p_comp_month#i.pt_ind
		
	marginsplot, xdimension(p_comp_month) ///
	plotdimension(pt_ind, elabels(1 "Putative Control Group" 2 "Putative Treatment Group")) ///
	recast(line) recastci(rarea) ///
	ciopts(fintensity(45) lcolor(%0)) ///
	ytitle(P(Removal)) xtitle(Proceeding Completion Month) ///
	xlabel(, format(%tmMonth_CCYY)) title("") subtitle("") caption("") note("")	
	
	graph export ///
	matter_ab_parallel_trends.tif, ///
	width(3900) replace 

********************************************************************************
* PRO SE ANALYSIS
********************************************************************************
/*	The effect of Matter of A-B- appears to be limited to represented asylum seekers.
	Among pro se cases, the removal rate in the period immediately prior to Matter 
	of A-B- was 97.3 percent. In the period immediately after Matter of A-B-, 
	the removal rate for this same group was 97 percent. For represented cases, 
	on the other hand, the removal rate immediately prior to Matter of A-B- was
	59.6 percent while it was 62 percent immediately after Matter of A-B-.  
	Given the lack of concurrent change among pro se cases, the data suggest
	that the introduction of Matter of A-B- did not significantly affect the 
	outcomes for pro se respondents, whose success rates were already notably 
	low. As we have noted in the manuscript, we expect IJs’ discretion to be 
	more likely to matter for represented cases because making the types of 
	claims that Matter of A-B- affected likely requires a level of legal 
	sophistication that an average pro se respondent lacks. */
	
*need to check bandwidth assumptions again 	
    rdbwselect removal_decision days_from_ab ///
	if nationality!=0 & ///
	app_asylum==1 & pandemic_MAR1==0, all

*trying out 136 days on either side 
*actual means 	

log using matter_ab_pro_se_mean_comparisons.log, replace 
	
	mean removal_decision if days_from_ab>-137 & days_from_ab<137 & ///
	is_missing==0 & IJ_pre_post_AB==1, ///
	over(atty matter_of_ab_an)
	reg removal_decision days_from_ab i.atty##i.matter_of_ab_an if days_from_ab>-137 & days_from_ab<137 & ///
	is_missing==0 & IJ_pre_post_AB==1
	
log close 
		
********************************************************************************
* FALSIFICATION TEST: GROUPS UNLIKELY TO HAVE BEEN AFFECTED
********************************************************************************
/*	We performed the following falsification test to obtain greater confidence
	that the pre and post Matter of A-B- discontinuity that we report in the 
	manuscript was indeed the result of the issuance of Matter of A-B-. We 
	re-estimated our models using a sample of only those proceedings that were
	not “at risk” of being impacted by Matter of A-B-. These are proceedings 
	in which (1) the noncitizen had not applied for asylum or withholding; (2) 
	the noncitizen was not from El Salvador, Guatemala, Honduras, or Mexico; a
	nd (3) the noncitizen did not have legal representation. If the observed 
	shifts in judicial decision-making that we report were unrelated to the 
	issuance of Matter of A-B- but instead, a response to some other policy 
	or unobserved external influence whose timing coincided with the issuance
	of Matter of A-B-, we might expect to see a significant increase in 
	removal-order rates among these proceedings that were not “at risk” of
	being affected by Matter of A-B-. */

*BINARY IJ CHARACTERISTICS 

log using matter_ab_shouldnt_be_affected.log, replace 
	
	reg removal_decision ///
	i.lang i.custody_n i.nat_fe ///
	cube_rt_caseload i.IJ_FE_ID ///
	i.matter_of_ab_an ///
	days_from_ab ///
	if days_from_ab>-61 & days_from_ab<61 & ///
	is_missing==0 & IJ_pre_post_AB==1 & shouldnt_be_affected==1, robust

log close 	
	
*Subset to proceedings that fit the criteria of being most likely to be affected
*by matter of a-b- 

keep if app_asylum==1 & nationality!=0 & atty==1
	
********************************************************************************
* PLACEBO MATTER OF A-B- TREATMENTS
********************************************************************************
/*	“-10 Days” estimates assume, counterfactually, that Matter of A-B- went into
	effect 10 days earlier, “-20 Days” assumes Matter of A-B- went into effect 
	20 days earlier, and so forth. */

*-10 Days
	gen days_from_fake_ab = days_from_ab-10
	gen fake_matter_of_ab_an =days_from_fake_ab>-1

log using matter_ab_placebo_10minus.log, replace 

	logit removal_decision ///
		  i.lang i.custody_n i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.fake_matter_of_ab_an#1.apt_dem_pres ///
	      fake_matter_of_ab_an ///
		  days_from_fake_ab ///
		  if days_from_fake_ab>-61 & days_from_fake_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)

log close 
	
*-20 Days
	replace days_from_fake_ab = days_from_ab-20
	replace fake_matter_of_ab_an =days_from_fake_ab>-1
	
log using  matter_ab_placebo_20minus.log, replace 

	logit removal_decision ///
		  i.lang i.custody_n i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.fake_matter_of_ab_an#1.apt_dem_pres ///
		  fake_matter_of_ab_an ///
		  days_from_fake_ab ///
		  if days_from_fake_ab>-61 & days_from_fake_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)

log close 

*-30 Days
	replace days_from_fake_ab = days_from_ab-30
	replace fake_matter_of_ab_an =days_from_fake_ab>-1
	
log using  matter_ab_placebo_30minus.log, replace 

	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID    ///
		  1.fake_matter_of_ab_an#1.apt_dem_pres ///
		  fake_matter_of_ab_an ///
		  days_from_fake_ab ///
		  if days_from_fake_ab>-61 & days_from_fake_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)

log close 	
		
*-365 Days
	replace days_from_fake_ab = days_from_ab-365
	replace fake_matter_of_ab_an =days_from_fake_ab>-1
	
log using matter_ab_placebo_365minus.log, replace 
		
	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.fake_matter_of_ab_an#1.apt_dem_pres ///
		  fake_matter_of_ab_an ///
		  days_from_fake_ab ////
		  if days_from_fake_ab>-61 & days_from_fake_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)

log close 			
			
********************************************************************************
* BANDWIDTH ASSUMPTIONS
********************************************************************************
/*	For the Matter of A-B- results presented in the manuscript, we compared 
	the 60 days pre and post the issuance of Matter of A-B-. We relied on
	data-driven bandwidth selection methods available in software developed by
	Calonico et al. (2017) to inform our decision to select this bandwidth.  
	Our choice of the 60-day bandwidth was based on a common coverage error-rate
	optimal bandwidth selector. We checked that our analysis results were robust
	to the use of a 100-day and 95-day bandwidth generated by a common mean squared-error 
	optimal bandwidth selector.  We present the results from the 60-day bandwidth 
	in the manuscript because observations are more likely to be similar the closer
	they are to the cutoff date of June 11, 2018.*/
 
log using matter_ab_bandwidth_output.log, replace 	
 
	rdbwselect removal_decision days_from_ab ///
	if atty==1 & nationality!=0 & ///
	app_asylum==1 & pandemic_MAR1==0,  all

log close 		
			
*100-DAY BANDWIDTH  
*BINARY IJ CHARACTERISTICS 
foreach var in hispanic_ij female {
		
log using  matter_ab_`var'100bw.log, replace 
		
	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  days_from_ab ///
		  if days_from_ab>-101 & days_from_ab<101 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
		  
	margins `var'#matter_of_ab_an, post
		  mlincom, clear 
		  mlincom (4-3), ///
		  stat(est se p) add ///
		  rowname("AB `var'=1")		
		  mlincom (2-1), ///
		  stat(est se p) add ///
		  rowname("AB `var'=0 DIF")				
  		  mlincom (4-3) -(2-1), ///
		  stat(est se p) add ///
		  rowname("DD")
		  
log close 
}

*95-DAY BANDWIDTH  
*BINARY IJ CHARACTERISTICS 
foreach var in hispanic_ij female {

log using  matter_ab_`var'95bw.log, replace 

	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  days_from_ab ///
		  if days_from_ab>-96 & days_from_ab<96 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
	
	margins `var'#matter_of_ab_an, post
		  mlincom, clear 
		  mlincom (4-3), ///
		  stat(est se p) add ///
		  rowname("AB `var'=1")		
		  mlincom (2-1), ///
		  stat(est se p) add ///
		  rowname("AB `var'=0 DIF")				
  		  mlincom (4-3)-(2-1), ///
		  stat(est se p) add ///
		  rowname("DD")
		  
log close 
}

********************************************************************************
* CIRCUIT FEs
********************************************************************************
/*	Geography may confound our results, as caselaw applicable to immigration 
	adjudication may vary across federal judicial circuits. This is relevant
	because the noncitizens most likely to be affected by Matter of A-B- are 
	unlikely to be randomly or evenly distributed across these jurisdictions. 
	Our main analysis uses judge fixed effects, which largely control for 
	jurisdiction-specific factors that remain constant over time.  
	However, to more directly address possible variations in laws 
	across jurisdictions, we also re-estimated the models using fixed 
	effects for federal judicial circuits.*/

*BINARY IJ CHARACTERISTICS 
foreach var in  hispanic_ij female {
		
log using  matter_ab_`var'_circuit_FE.log, replace 
	
	logit removal_decision ///
		  i.lang i.custody_n i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID i.circuit_n ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  days_from_ab ///
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
		
	margins `var'#matter_of_ab_an, post
		  mlincom, clear 
		  mlincom (4-3), ///
		  stat(est se p) add ///
		  rowname("AB `var'=1")		
		  mlincom (2-1),  ///
		  stat(est se p) add ///
		  rowname("AB `var'=0 DIF")				
  		  mlincom (4-3)-(2-1),  ///
		  stat(est se p) add ///
		  rowname("DD")
		  
log close 
}

********************************************************************************
*IJ WORK EXPERIENCE
********************************************************************************
/*	IJs’ pre-appointment career experiences may shape how they respond to the
	types of outside influences that we examine in this study. Furthermore, 
	judges who are Asian, Hispanic, female, or who preside in courts that are 
	especially diverse in terms of values for Percent Asian Court, Percent 
	Hispanic Court, or Percent Female Court, may systematically vary in their
	pre-appointment career experiences compared to their counterparts.  
	We thus re-estimated our models that includedinteraction terms between 
	the Matter of A-B- and indicators for each of the following, respectively:
	(1) whether a given IJ ever worked in an NGO setting prior to appointment,
	and (2) whether a given IJ ever worked for the DHS (or its predecessor
	agency, Immigration and Naturalization Service) prior to appointment. */
	
*NGO
foreach var in hispanic_ij female {

log using  matter_ab_`var'_change_ngo.log, replace 

	logit removal_decision ///
		  i.lang i.custody_n i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  1.ever_ngo#1.matter_of_ab_an ///
		  days_from_ab ///
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
		
	margins `var'#matter_of_ab_an, post
		  mlincom, clear 
		  mlincom (4-3), ///
		  stat(est se p) add ///
		  rowname("AB `var'=1")		
		  mlincom (2-1), ///
		  stat(est se p) add ///
		  rowname("AB `var'=0 DIF")				
  		  mlincom (4-3)-(2-1), ///
		  stat(est se p) add ///
		  rowname("DD")

log close 
}

*INS/DHS
foreach var in hispanic_ij female {

log using  matter_ab_`var'_change_ins_dhs.log, replace 
	
	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  1.ever_ins_dhs#1.matter_of_ab_an ///
		  days_from_ab ///
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
		
	margins `var'#matter_of_ab_an, post
		  mlincom, clear 
		  mlincom (4-3), ///
		  stat(est se p) add ///
		  rowname("AB `var'=1")		
		  mlincom (2-1), ///
		  stat(est se p) add ///
		  rowname("AB `var'=0 DIF")				
  		  mlincom (4-3)-(2-1),  ///
		  stat(est se p) add ///
		  rowname("DD")

log close 
}

********************************************************************************
* CASTRO TUM AS A POTENTIAL CONFOUNDER
********************************************************************************
/*	The Castro-Tum decision, issued by Attorney General Sessions on May 17, 2018,
	removed IJs’ authority to administratively close cases. This ruling had the
	potential to significantly affect the management of IJs’ dockets along with 
	the adjudication speed and outcome of asylum cases. Given that Castro-Tum 
	fell within our pre-Matter of A-B study period, there is a potential for 
	confounding effects, wherein shifts in case outcomes post-Matter of A-B- 
	might partially reflect responses to Castro-Tum. To mitigate this risk, 
	we conducted robustness checks by re-estimating our models to account for 
	any independent influence that Castro-Tum might have had. Specifically, 
	we included indicator variables for the two significant dates associated 
	with Castro-Tum: May 17, 2018 (when the decision was issued) and July 26, 
	2018 (when EOIR assigned a new judge to manage the backlog resulting from 
	the decision).*/
 
gen castro_tum1 = p_comp_date > date("20180516","YMD")
gen castro_tum2 = p_comp_date > date("20180725","YMD")

foreach var in hispanic_ij female {

log using  matter_ab_`var'_castro_tum.log, replace 
		
	logit removal_decision castro_tum1 castro_tum2 ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  days_from_ab ///
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
		  
	margins `var'#matter_of_ab_an, post
		  mlincom, clear 
		  mlincom (4-3), ///
		  stat(est se p) add ///
		  rowname("AB `var'=1")		
		  mlincom (2-1), ///
		  stat(est se p) add ///
		  rowname("AB `var'=0 DIF")				
  		  mlincom (4-3)-(2-1), ///
		  stat(est se p) add ///
		  rowname("DD")
		  
log close 
}

*******************************************************************************
*BALANCE CHECK
********************************************************************************
/*	Compare the characteristics of individuals just above and below
	the cutoff to assess the balance in observed covariates. The balance should 
	indicate that individuals on both sides of the cutoff are similar, which 
	supports the assignment assumption. Statistical tests, such as t-tests or 
	chi-square tests, can be used to assess the differences in covariate means 
	or distributions.*/

log using matter_ab_balance_check.log, replace
	
*categorical vars 
	foreach var in ij_asian hispanic_ij female ///
	female custody_n lang nat_fe {
	tab `var' matter_of_ab_an ///
	if days_from_ab>-61 & days_from_ab<61 & ///
	is_missing==0 & IJ_pre_post_AB==1, chi row col 
}

*continous vars 
	foreach var in percent_asian_14 percent_hispanic_14 ///
	percent_female_14 cube_rt_caseload {
	di "`var'"
	ttest `var' if days_from_ab>-61 & days_from_ab<61 & ///
	is_missing==0 & IJ_pre_post_AB==1, by(matter_of_ab_an) 
}

log close

	label var ij_asian "Asian IJ"
	label var hispanic_ij "Hispanic IJ"
	label var female "Female IJ"
	label var apt_dem_pres "Democratic Apointee"
	label var percent_asian_14 "Percent Asian Court"
	label var percent_hispanic_14 "Percent Hispanic Court"
	label var percent_female_14 "Percent Female Court"
	label var cube_rt_caseload "Total Caseload"
	label var atty "Represented"
	
	gen nat_fe2 = "El Salvador" if nat_fe==72
	replace nat_fe2 = "Guatemala" if nat_fe==92
	replace nat_fe2 = "Honduras" if nat_fe==100
	replace nat_fe2 = "Mexico" if nat_fe==149
	
	encode nat_fe2, gen(nat_fe_n)
	
	reg matter_of_ab_an ///
		ij_asian hispanic_ij female ///
		percent_asian_14 percent_hispanic_14 ///
		percent_female_14 apt_dem_pres atty cube_rt_caseload ///
		i.lang i.custody_n i.nat_fe_n ///
		if days_from_ab>-61 & days_from_ab<61 & ///
		is_missing==0 & IJ_pre_post_AB==1, cluster(IJ_FE_ID)

	coefplot, drop(_cons) xline(0, lpattern(dash)) ///
    xlabel(, valuelabel) ///
    text(0 -0.05 "", placement(e)) ///
    title("Change P({it:Matter of A-B-} Treatment Period)") ///
    graphregion(margin(0 0 2 0)) // Increase the top margin as needed

	graph export ///
	matter_ab_predictors.tif, ///
	width(3900) replace

*BALANCE ADJUSTMENT
foreach var in  hispanic_ij female {

log using  matter_ab_`var'_balance_adjust.log, replace

*adjusted model 
	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  1.custody_n#1.matter_of_ab_an ///
		  c.cube_rt_caseload#1.matter_of_ab_an ///
		  days_from_ab ///
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
		  
log close 					
}	

********************************************************************************
* Pre-treatment Trends: Time-invariant IJ characteristics
********************************************************************************
/*  A key assumption of our approach that examines interactions 
	between extralegal influences and time-invariant IJ characteristics 
	is that IJs of different genders and racial identities would have 
	followed the same trend in removal rates in the absence of treatment.  
	We tested this assumption by breaking up days leading up to 
	Matter of A-B- into groups of 10 days and interacting these day groups 
	with Hispanic IJ and Female IJ (i.e., the significant  time-invariant 
	IJ characteristics of interest to this study).*/
 
	gen day_group = 1 if  days_from_ab <-49
	replace day_group = 2 if days_from_ab <-39 & missing(day_group)
	replace day_group = 3 if days_from_ab <-29 & missing(day_group)
	replace day_group = 4 if days_from_ab <-19 & missing(day_group)
	replace day_group = 5 if days_from_ab <-9 & missing(day_group)
	replace day_group = 6 if days_from_ab <0 & missing(day_group)
 
foreach var in hispanic_ij female {

log using  matter_ab_`var'_pre_trends.log, replace
	
	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  i.day_group ///
		  1.`var'#2.day_group ///
		  1.`var'#3.day_group ///
		  1.`var'#4.day_group ///
		  1.`var'#5.day_group ///
		  1.`var'#6.day_group ///
		  if days_from_ab>-61 & days_from_ab<0 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)

log close
}

********************************************************************************
* FUNCTIONAL FORM SPECIFICATION
********************************************************************************
/*	In our main analysis, we use a linear specification where time is treated
	as a continuous variable. Here we test alternative functional forms: 
	Explore alternative functional forms for the running variable to 
	assess their influence on the treatment effect estimates. 
	Common alternatives include polynomial functions, fractional polynomial
	functions, spline functions, or local linear functions.*/
		
*categorical vars 
foreach var in female ij_asian hispanic_ij {
	
	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.apt_dem_pres#1.matter_of_ab_an ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  c.days_from_ab##c.days_from_ab /// 
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
		  margins `var', dydx(matter_of_ab_an) post 
	
	mlincom, clear 
	qui mlincom (2)-(1), ///
		stat(est ul ll ) add ///
		rowname("DD")
		gen `var'_e = _mlincom[1, 1] 
		gen `var'_ul = _mlincom[1, 2] 
		gen `var'_ll = _mlincom[1, 3] 
			
}
*continuous var
foreach var in  percent_female_14 {
	
	reg removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		c.days_from_ab##c.days_from_ab /// 
		if days_from_ab>-61 & days_from_ab<61  ///
		& is_missing==0 & IJ_pre_post_AB==1 & female==0, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & female==0, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' // Rounding to integer
		local median_SD = `median_val' + `SD'
		
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 

}

foreach var in  percent_hispanic_14 percent_asian_14 {

	reg removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		i.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		c.days_from_ab##c.days_from_ab /// 
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & white_ij==1, cluster(nat_fe)
		sum `var' if days_from_ab>-61 & days_from_ab<61 & white_ij==1, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
		
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 

}
*store output from margins 
	frame put /// 
	ij_asian_ll hispanic_ij_ll female_ll ///
	percent_asian_14_ll percent_hispanic_14_ll percent_female_14_ll ///
	ij_asian_ul hispanic_ij_ul female_ul ///
	percent_asian_14_ul percent_hispanic_14_ul percent_female_14_ul ///
	ij_asian_e hispanic_ij_e  female_e ///
	percent_asian_14_e percent_hispanic_14_e percent_female_14_e, ///
	into(fig_1)
 
*make frame to plot stats 
	frame change fig_1
	do matter_of_ab_figure.do
	graph export ///
	matter_of_ab_dif_squared.tif, ///
	width(3900) replace 
		
	gen f_name = "Squared"		
		
*******************************************************************************
* CUBED
********************************************************************************
	
	frame change default 
	drop female_e female_ul female_ll ij_asian_e ij_asian_ul ///
		 ij_asian_ll hispanic_ij_e hispanic_ij_ul ///
		 hispanic_ij_ll percent_female_14_e ///
		 percent_female_14_ul percent_female_14_ll ///
		 percent_hispanic_14_e percent_hispanic_14_ul ///
		 percent_hispanic_14_ll percent_asian_14_e ///
		 percent_asian_14_ul percent_asian_14_ll
			
*categorical vars 
foreach var in female ij_asian hispanic_ij {
	
	logit removal_decision ///
		  i.lang i.custody_n i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.apt_dem_pres#1.matter_of_ab_an ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  c.days_from_ab##c.days_from_ab##c.days_from_ab /// 
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
	
	margins `var', dydx(matter_of_ab_an) post 
		    mlincom, clear 
			qui mlincom (2)-(1), ///
			stat(est ul ll ) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
			
}
*continuous var
foreach var in percent_female_14 {
		
	reg removal_decision ///
		i.lang i.custody_n i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		c.days_from_ab##c.days_from_ab##c.days_from_ab /// 
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & female==0, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & female==0, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e =  _mlincom[1, 1] 
			gen `var'_ul =  _mlincom[1, 2] 
			gen `var'_ll =  _mlincom[1, 3] 
}

foreach var in percent_hispanic_14 percent_asian_14 {
	
	reg removal_decision ///
		i.lang i.custody_n i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		i.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		c.days_from_ab##c.days_from_ab##c.days_from_ab /// 
		if days_from_ab>-61 & days_from_ab<61  ///
		& is_missing==0 & IJ_pre_post_AB==1 & white_ij==1, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & white_ij==1, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
		
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}
*store output from margins 
	
	frame put /// 
	ij_asian_ll hispanic_ij_ll female_ll ///
	percent_asian_14_ll percent_hispanic_14_ll percent_female_14_ll ///
	ij_asian_ul hispanic_ij_ul female_ul ///
	percent_asian_14_ul percent_hispanic_14_ul percent_female_14_ul ///
	ij_asian_e hispanic_ij_e  female_e ///
	percent_asian_14_e percent_hispanic_14_e percent_female_14_e, ///
	into(fig_2)
 
*make frame to plot stats 
	frame change fig_2
	do matter_of_ab_figure.do
	graph export ///
	matter_of_ab_dif_cubed.tif, ///
	width(3900) replace 
		
	gen f_name = "Cubic"		
		
********************************************************************************
* Spline w/ 6 breaks 
********************************************************************************
	
	frame change default 
	drop female_e female_ul female_ll ij_asian_e ij_asian_ul ///
		 ij_asian_ll hispanic_ij_e hispanic_ij_ul ///
		 hispanic_ij_ll percent_female_14_e ///
		 percent_female_14_ul percent_female_14_ll ///
		 percent_hispanic_14_e percent_hispanic_14_ul ///
		 percent_hispanic_14_ll percent_asian_14_e ///
		 percent_asian_14_ul percent_asian_14_ll
	
	mkspline day1 -40 day2 -20 day3 0 ///
		day4 20 day5 40 day6 = days_from_ab, marginal

*categorical vars 
foreach var in female ij_asian hispanic_ij {
	
	logit removal_decision ///
		  i.lang i.custody_n i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.apt_dem_pres#1.matter_of_ab_an ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  day1-day6 ///
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
	
	margins `var', dydx(matter_of_ab_an) post 
			mlincom, clear 
			qui mlincom (2)-(1), ///
			stat(est ul ll ) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 		
}

*continuous var
foreach var in percent_female_14 {
	
	reg removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		day1-day6 ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & female==0, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & female==0, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

foreach var in percent_hispanic_14 percent_asian_14 {
	reg removal_decision ///
		i.lang i.custody_n i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID    ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		i.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		day1-day6 ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & white_ij==1, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & white_ij==1, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

*store output from margins 
	frame put /// 
	ij_asian_ll hispanic_ij_ll female_ll ///
	percent_asian_14_ll percent_hispanic_14_ll percent_female_14_ll ///
	ij_asian_ul hispanic_ij_ul female_ul  ///
	percent_asian_14_ul percent_hispanic_14_ul percent_female_14_ul ///
	ij_asian_e hispanic_ij_e  female_e ///
	percent_asian_14_e percent_hispanic_14_e percent_female_14_e, ///
	into(fig_3)
 
*make frame to plot stats 
	frame change fig_3
	do matter_of_ab_figure.do
	graph export ///
	matter_of_ab_dif_sp6.tif, ///
	width(3900) replace 	
	gen f_name = "Spline with 6 Breaks"		
	
********************************************************************************
* Spline w/ 4 breaks 
********************************************************************************
	
	frame change default 
	drop female_e female_ul female_ll ij_asian_e ij_asian_ul ///
		 ij_asian_ll hispanic_ij_e hispanic_ij_ul ///
		 hispanic_ij_ll percent_female_14_e ///
		 percent_female_14_ul percent_female_14_ll ///
		 percent_hispanic_14_e percent_hispanic_14_ul ///
		 percent_hispanic_14_ll percent_asian_14_e ///
		 percent_asian_14_ul percent_asian_14_ll
	
		mkspline day1x -30  day2x 0 ///
		day3x 30 day4x = days_from_ab, marginal
		
*categorical vars 
foreach var in female ij_asian hispanic_ij {
	
	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.apt_dem_pres#1.matter_of_ab_an ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  day1x-day4x ///
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
			
	margins `var', dydx(matter_of_ab_an) post 
			mlincom, clear 
			qui mlincom (2)-(1), ///
			stat(est ul ll ) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 		
}

*continuous var
foreach var in  percent_female_14 {
	
	reg removal_decision ///
		i.lang i.custody_n i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		day1x-day4x ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & female==0, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & female==0, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

foreach var in  percent_hispanic_14 percent_asian_14 {
	
	reg removal_decision ///
		i.lang i.custody_n i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		i.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		day1x-day4x ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & white_ij==1, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & white_ij==1, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

*store output from margins 
	frame put /// 
	ij_asian_ll hispanic_ij_ll female_ll ///
	percent_asian_14_ll percent_hispanic_14_ll percent_female_14_ll ///
	ij_asian_ul hispanic_ij_ul female_ul ///
	percent_asian_14_ul percent_hispanic_14_ul percent_female_14_ul ///
	ij_asian_e hispanic_ij_e  female_e ///
	percent_asian_14_e percent_hispanic_14_e percent_female_14_e, ///
	into(fig_4)
 
*make frame to plot stats 
	frame change fig_4
	do matter_of_ab_figure.do
	graph export ///
	matter_of_ab_dif_sp4.tif, ///
	width(3900) replace 	
		
gen f_name = "Spline with 4 Breaks"
				
********************************************************************************
*Spline w/ 2 breaks 
********************************************************************************
	
	frame change default 
	drop female_e female_ul female_ll ij_asian_e ij_asian_ul ///
		 ij_asian_ll hispanic_ij_e hispanic_ij_ul ///
		 hispanic_ij_ll percent_female_14_e ///
		 percent_female_14_ul percent_female_14_ll ///
		 percent_hispanic_14_e percent_hispanic_14_ul ///
		 percent_hispanic_14_ll percent_asian_14_e ///
		 percent_asian_14_ul percent_asian_14_ll
	
	mkspline day1z  0 ///
		day2z  = days_from_ab, marginal
		
*categorical vars 
foreach var in female ij_asian hispanic_ij {
	
	logit removal_decision ///
		  i.lang i.custody_n i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.apt_dem_pres#1.matter_of_ab_an ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  day1z-day2z ///		
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
	
	margins `var', dydx(matter_of_ab_an) post 
			mlincom, clear 
			qui mlincom (2)-(1), ///
			stat(est ul ll ) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 	
}

*continuous var
foreach var in  percent_female_14 {
	
	reg removal_decision ///
		i.lang i.custody_n i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID    ///
		i.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		day1z-day2z ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & female==0, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & female==0, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

foreach var in  percent_hispanic_14 percent_asian_14 {
	
	reg removal_decision ///
		i.lang i.custody_n i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID    ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		i.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		day1z-day2z ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & white_ij==1, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & white_ij==1, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
			mlincom (4-3)-(2-1),  ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

*store output from margins 
	frame put /// 
	ij_asian_ll hispanic_ij_ll female_ll ///
	percent_asian_14_ll percent_hispanic_14_ll percent_female_14_ll ///
	ij_asian_ul hispanic_ij_ul female_ul ///
	percent_asian_14_ul percent_hispanic_14_ul percent_female_14_ul ///
	ij_asian_e hispanic_ij_e  female_e ///
	percent_asian_14_e percent_hispanic_14_e percent_female_14_e, ///
	into(fig_5)
 
*make frame to plot stats 
	frame change fig_5
	do matter_of_ab_figure.do
	graph export ///
	matter_of_ab_dif_sp2.tif, ///
	width(3900) replace 			
		
gen f_name = "Spline with 2 Breaks"

********************************************************************************
* LINEAR REGULAR
********************************************************************************
	
	frame change default 
	drop female_e female_ul female_ll ij_asian_e ij_asian_ul ///
		 ij_asian_ll hispanic_ij_e hispanic_ij_ul ///
		 hispanic_ij_ll percent_female_14_e ///
		 percent_female_14_ul percent_female_14_ll ///
		 percent_hispanic_14_e percent_hispanic_14_ul ///
		 percent_hispanic_14_ll percent_asian_14_e ///
		 percent_asian_14_ul percent_asian_14_ll
	
*categorical vars 
foreach var in female ij_asian hispanic_ij {
	
	logit removal_decision ///
		  i.lang i.custody_n  i.nat_fe ///
		  cube_rt_caseload i.IJ_FE_ID ///
		  1.apt_dem_pres#1.matter_of_ab_an ///
		  i.matter_of_ab_an ///
		  1.`var'#1.matter_of_ab_an ///
		  days_from_ab ///		
		  if days_from_ab>-61 & days_from_ab<61 & ///
		  is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
	
	margins `var', dydx(matter_of_ab_an) post 
			mlincom, clear 
			qui mlincom (2)-(1), ///
			stat(est ul ll ) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
			
}

*continuous var
foreach var in  percent_female_14 {
		
	reg removal_decision ///
		i.lang i.custody_n i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		days_from_ab ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & female==0, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & female==0, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1),  ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

foreach var in  percent_hispanic_14 percent_asian_14 {
	
	reg removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID  ///
		1.apt_dem_pres#1.matter_of_ab_an ///
		i.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		days_from_ab ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & white_ij==1, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & white_ij==1, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

*store output from margins 
	frame put /// 
	ij_asian_ll hispanic_ij_ll female_ll ///
	percent_asian_14_ll percent_hispanic_14_ll percent_female_14_ll ///
	ij_asian_ul hispanic_ij_ul female_ul ///
	percent_asian_14_ul percent_hispanic_14_ul percent_female_14_ul ///
	ij_asian_e hispanic_ij_e  female_e ///
	percent_asian_14_e percent_hispanic_14_e percent_female_14_e, ///
	into(fig_6)
 
*make frame to plot stats 
	frame change fig_6
	do matter_of_ab_figure.do
	graph export ///
	matter_of_ab_dif_linear.tif, ///
	width(3900) replace 			
	gen f_name = "Linear"
	
********************************************************************************
* Combine frames plots 
********************************************************************************
*save the base frame (fig_1) to the dataset
	frame change fig_1
	tempfile base_data
	save `base_data'

*loop over the remaining frames and append them to the base dataset
	local frames fig_2 fig_3 fig_4 fig_5 fig_6
	
foreach frame in `frames' {
    frame change `frame'
    append using `base_data'
    save `base_data', replace
}

*load the final appended dataset back into a frame
	frame create combined_frame
	use `base_data', clear

	gen order_s = 1 if f_name=="Linear"
	replace order_s=2 if f_name=="Squared"
	replace order_s=3 if f_name=="Cubic"
	replace order_s=4 if f_name=="Spline with 2 Breaks"
	replace order_s=5 if f_name=="Spline with 4 Breaks"
	replace order_s=6 if f_name=="Spline with 6 Breaks"
 
*define the value label
	label define order_labels 1 "Linear" ///
          2 "Squared" ///
          3 "Cubic" ///
          4 "Spline with 2 Breaks" ///
          5 "Spline with 4 Breaks" ///
          6 "Spline with 6 Breaks"

*add value label to the variable
	label values order_s order_labels

*ACTUAL FIGURE
	twoway (rcap U_CI L_CI var_idn) ///
	(scatter Difference var_idn, mcolor(black) ///
	msymbol(circle) mlabel(var_idn) ///
	mlabcolor(black) mlabposition(6) ///
	mlabangle(vertical)), ///
	ytitle("Differences in {it:Matter of A-B-} Effects") ///
	yline(0) xtitle("") ///
	xlabel(1(1)6.5, labels labcolor(none) noticks) ///
	by(, title("") subtitle("") caption("") note("")) ///
	by(, legend(off)) by(order_s) ///
	subtitle(, position(11) nobox) plotregion(lcolor(black))
		
	graph export ///
	matter_of_ab_dif_functional_form.tif, ///
	width(3900) replace 
